package cn.lee.wechat.test.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.lee.wechat.bean.User;
import cn.zhouyafeng.itchat4j.api.MessageTools;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@RestController
public class WeChatDemoTestController {

	private Logger logger = Logger.getLogger(WeChatDemoTestController.class);
	
	@RequestMapping("/user")
	public String getUserList(){
		List<String> contactList = WechatTools.getContactList();
		StringBuilder sb = new StringBuilder(contactList.size());
		for (String string : contactList) {
			sb.append(string+"  ");
		}
		return sb.toString();
	}
	
	@RequestMapping("/id")
	public String getUserIds(){
		List<String> contactList = WechatTools.getContactList();
		StringBuilder sb = new StringBuilder(contactList.size());
		for (String string : contactList) {
			sb.append(WechatTools.getUserNameByNickName(string));
		}
		return sb.toString();
	}
	
	@RequestMapping("/userInfo")
	public List<User> getUserInfoByJSON(){
		List<String> contactList = WechatTools.getContactList();
		
		List<User> userList = new ArrayList<>(contactList.size());
		for (String string : contactList) {
			User user = new User(string,WechatTools.getUserNameByNickName(string));
			userList.add(user);
		}
		return userList;
	}
	
	@RequestMapping("/sendMessage")
	public String sendMessage(){
		String text="睡麻痹，起来嗨！";
		MessageTools.sendMsgByNickName(text, "北风呼呼地吹");
		return "给老贺发送消息成功"; 
	}
	
	
	/**
	 * 根据用户昵称发送消息；注意：不是微信号也不是备注，而是昵称
	 * @return
	 * @throws InterruptedException 
	 */
	@RequestMapping("/message")
	public String sendMessage2All() throws InterruptedException{
		String text="测试自动发送消息：我不要多少，我就要20万！";
//		String name = WechatTools.getUserNameByNickName("Eric");
		String name2= WechatTools.getUserNameByNickName("我就是我不一样的烟火");
		for (int i = 0; i < 100; i++) {
			Thread.sleep(3000);
			//MessageTools.sendMsgById(text, name);
			MessageTools.sendMsgById(text, name2);
		}			
		
		return "SUCCESS";
	}
	
	/**
	 * 获取群列表
	 * @return
	 */
	@RequestMapping("/group")
	public  List<JSONObject> getGroup(){
		List<JSONObject> groupList = WechatTools.getGroupList();
		logger.info("查询微信群列表");
		return groupList;
	}
}
